var runtime = require('./index')

// var qcloud = require('./libs/wafer2-client-sdk/index.js')
// var config = require('../config.js')

const screenWidth  = window.innerWidth
const screenHeight = window.innerHeight

let atlas = new Image()
atlas.src = 'images/Common.png'

export default class GameInfo {

  constructor(){
    /**
     * 重新开始按钮区域
     * 方便简易判断按钮点击
     */
    this.btnArea = {
      startX: screenWidth / 2 - 40,
      startY: screenHeight / 2 - 100 + 180,
      endX: screenWidth / 2 + 50,
      endY: screenHeight / 2 - 100 + 255
    }
    this.endTime = 0
    this.flag = false
  }

  /**
   * 向后台发送得分
   */
  sendScore(ctx, score) {
    runtime.sendMessage('rscore', { 'score': score })
  }

  /**
   * 实时渲染得分
   */
  renderGameScore(ctx, score) {
    // 设定字体
    ctx.fillStyle = "#ffffff"
    ctx.font = "20px Arial"

    // 判断有没有登录，登录显示昵称，未登录显示“当前得分”
    var usernicname = _globalData.userInfo.nickName ? _globalData.userInfo.nickName : '当前得分'
    ctx.fillText(
      usernicname + ': ' + score,
      10,
      60
    )

    if (_globalData.realTimeRank && _globalData.realTimeRank.length > 0) {
      _globalData.realTimeRank.forEach(function (value, index) {
        if (value.username != _globalData.userInfo.nickName) {
          //不在这渲染自己的分数
          ctx.fillText(
            value.username + '得分: ' + value.score,
            10,
            40 + (index + 1) * 20
          );
        }
      })
    } 
  }

  /**
   * 渲染游戏结束
   */
  renderGameOver(ctx, score, startTime) {

    // 以下代码预留，在 main.js 中有了，如果排行榜不行的话，放开；
    // wx.setUserCloudStorage({
    //   KVDataList: ['score'],
    //   success: res => { // {errMsg: "getFriendCloudStorage:ok", data: Array(0)}
    //     console.log(res)
    //     if (res.errMsg == "getFriendCloudStorage:ok") {
          
    //     } else {

    //     }
    //   },
    //   fail: res => {
    //     console.log('fail' + JSON.stringify(res))
    //   },
    // })
  
    ctx.drawImage(atlas, 0, 0, 119, 108, screenWidth / 2 - 150, screenHeight / 2 - 100, 300, 300)

    ctx.fillStyle = "#ffffff"
    ctx.font = "20px Arial"

    if (score == 99) {
      ctx.fillText(
        'Congratulations！',
        screenWidth / 2 - 60,
        screenHeight / 2 - 100 + 50
      )
    } else {
      ctx.fillText(
        'Game over！',
        screenWidth / 2 - 40,
        screenHeight / 2 - 100 + 50
      )
    }

    if (_globalData.userInfo && _globalData.userInfo.nickName) {
      ctx.fillText(
        '     得分: ' + score,
        screenWidth / 2 - 60,
        screenHeight / 2 - 100 + 130
      )
    }

    ctx.drawImage(
      atlas,
      120, 6, 39, 24,
      screenWidth / 2 - 60,
      screenHeight / 2 - 100 + 180,
      120, 40
    )

    // 渲染游戏结束的时候为结束时间
    if(!this.flag){
      this.endTime = new Date().getTime()
      this.flag = true
    }

    var duration = (parseFloat(this.endTime) - parseFloat(startTime))/1000
    _globalData.usetime = duration + ""

    ctx.fillText(
      '耗时:' + duration + '秒',
      screenWidth / 2 - 60,
      screenHeight / 2 - 100 + 163
    )

    ctx.fillText(
      '重新开始',
      screenWidth / 2 - 40,
      screenHeight / 2 - 100 + 205
    )

    /**
     * 重新开始按钮区域
     * 方便简易判断按钮点击
     */
    this.btnArea = {
      startX: screenWidth / 2 - 40,
      startY: screenHeight / 2 - 100 + 180,
      endX: screenWidth / 2 + 50,
      endY: screenHeight / 2 - 100 + 255
    }

  }
}

